package com.ybg.bookrules.commons;


import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.nio.charset.StandardCharsets;

@Component
@Scope("prototype")//原型模式，来一个对象就创建一个
public class BoomValication {

    @Value("${boom.datasize}")
    private int datasize;
    @Value("${boom.fpp}")
    private double fpp;

    private BloomFilter<String> bloomFilter;

/*    public BoomValication() {
        //this.bloomFilter = BloomFilter.create(Funnels.stringFunnel(StandardCharsets.UTF_8),datasize, fpp);
    }*/
    //javax 当本类构建对象时 立即执行一次的初始化方法
    @PostConstruct
    public void init() {
        this.bloomFilter = BloomFilter.create(Funnels.stringFunnel(StandardCharsets.UTF_8), datasize, fpp);
    }

    public BloomFilter<String> getBloomFilter() {
        return bloomFilter;
    }

    //向布隆过滤器中填充新的数据
    public void put(String usernameAndPassword) {
        bloomFilter.put(usernameAndPassword);
    }

    //在布隆过滤器中查询数据是否存在
    public boolean exists(String val) {
        return bloomFilter.mightContain(val);
    }
}


